//
//  BranchHeader.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/7.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

protocol BranchHeaderDelegate: NSObjectProtocol {
    func selectAllFromBranch(sender: UIButton)
}

class BranchHeader: UIView {

    var delegate: BranchHeaderDelegate!
    
    // MARK: - life cycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initBaseLayout()
        layoutPageSubviews()
        self.backgroundColor = UIColor.white
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - event response
    func selectAllFromBranch(sender: UIButton) {
        if self.delegate.responds(to: #selector(BranchHeader.selectAllFromBranch(sender:))) {
            
            self.delegate.selectAllFromBranch(sender: sender)
        }
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.addSubview(selectedButton)
        self.addSubview(imageView)
        self.addSubview(branchLabel)
    }
    
    func layoutPageSubviews() {
        selectedButton.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(5)
            make.centerY.equalTo(self)
            make.width.height.equalTo(32)
        }
        
        imageView.snp.makeConstraints { (make) in
            make.left.equalTo(selectedButton.snp.right).offset(2)
            make.centerY.equalTo(self)
            make.width.height.equalTo(14)
        }
        
        branchLabel.snp.makeConstraints { (make) in
            make.left.equalTo(imageView.snp.right).offset(3)
            make.centerY.equalTo(self)
            make.width.equalTo(170)
            make.height.equalTo(16)
        }
    }
    
    // MARK: - setters and getters
    var _section: Int?
    var section: Int? {
        set {
            if newValue == 0 {
                branchLabel.text = "话梅国内店"
            } else if newValue == 1 {
                branchLabel.text = "话梅香港店"
            }
            selectedButton.tag = newValue!+10
        } get {
            return _section
        }
    }
    
    var _selectedButton: UIButton!
    var selectedButton: UIButton {
        if _selectedButton == nil {
            _selectedButton = UIButton()
            _selectedButton.setBackgroundImage(UIImage(named: "icon_select_n"), for: .normal)
            _selectedButton.setBackgroundImage(UIImage(named: "icon_select_h"), for: .highlighted)
            _selectedButton.setBackgroundImage(UIImage(named: "icon_select_s"), for: .selected)
            _selectedButton.addTarget(self, action: #selector(BranchHeader.selectAllFromBranch(sender:)), for: .touchUpInside)
        }
        return _selectedButton
    }
    
    var _imageView: UIImageView!
    var imageView: UIImageView {
        if _imageView == nil {
            _imageView = UIImageView()
            _imageView.image = UIImage(named: "tab_shop_s")
        }
        return _imageView
    }
    
    var _branchLabel: UILabel!
    var branchLabel: UILabel {
        if _branchLabel == nil {
            _branchLabel = UILabel()
            _branchLabel.font = UIFont(name: "PingFangSC-Light", size: 14)
            _branchLabel.textColor = UIColor(hexString: "#252525")
        }
        return _branchLabel
    }
    
}
